package com.bnz.controller;


import com.bnz.entity.Student;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.bnz.vo.StuVo;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/stupage")
public class StuByPage extends HttpServlet {

    private StudentService studentService;


    public void init() throws ServletException {

        studentService = new StudentServiceImpl();
    }
  /* @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        listByPage(req,resp);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        listByPage(req,resp);
    }*/

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        listByPage(req,resp);
    }

    public void listByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        StuVo stu = new StuVo();
        if(req.getParameter("name")!=null ){
            stu.setName(req.getParameter("name"));

        }else {
            stu.setName("");
        }
        if(req.getParameter("age") != null && req.getParameter("age") !="" ){
            stu.setAge(Integer.valueOf(req.getParameter("age")));

        }


        Integer pageNo = req.getParameter("pageNo") ==null || req.getParameter("pageNo")=="" ? 1:Integer.valueOf(req.getParameter("pageNo"));

        Integer pageSize = req.getParameter("pageSize") ==null ? 5:Integer.valueOf(req.getParameter("pageSize"));

        PageInfo pageInfo = studentService.queryByPage(stu,pageNo,pageSize);

        req.setAttribute("students",pageInfo.getList());
        req.setAttribute("nowPage",pageInfo.getPages());
        req.setAttribute("pagename",stu.getName());
        req.setAttribute("pageage",stu.getAge());
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);

    }
}
